/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 12/23/2010 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using BusinessObject;
using Utility;
using System.Xml;
namespace DataAccessLayer
{
    /// <summary>
    /// Define PhieuNhapHangDaoBase class. This class user for DataAccessLayer, contain 4 function Insert, Update, Search, Delete.
    /// If you want add new function, you must define in PhieuNhapHangDao class , not here.
    /// </summary>
    public class PhieuNhapHangDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public PhieuNhapHangDaoBase() { }
        static PhieuNhapHangDaoBase()
        {
            //Init logger.
            logger = LoggingUtility.GetLogger(typeof(PhieuNhapHangDaoBase).FullName);
        }
        #endregion
        /// <summary>
        /// Read data from IDataReader, convert to PhieuNhapHang Object.
        /// </summary>
        private static PhieuNhapHang GetPhieuNhapHangByDataReader(IDataReader reader)
        {
            String methodName = "GetPhieuNhapHangByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                PhieuNhapHang result = new PhieuNhapHang();
                //read MaPhieuNhapHang field data.

                result.MaPhieuNhapHang = ManagerDao.Getint64DataFromDataReader(reader, "MaPhieuNhapHang");

                //read NguoiNhap field data.

                result.NguoiNhap = ManagerDao.GetstringDataFromDataReader(reader, "NguoiNhap");

                //read MaKho field data.

                result.MaKho = ManagerDao.GetintDataFromDataReader(reader, "MaKho");
                result.MaNhaCC = ManagerDao.GetintDataFromDataReader(reader, "MaNCC");

                //read NgayNhap field data.

                result.NgayNhap = ManagerDao.GetdatetimeDataFromDataReader(reader, "NgayNhap");
                result.IsCommit = ManagerDao.GetbooleanDataFromDataReader(reader, "IsCommit");
                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        /// <summary>
        /// Define function Insert for PhieuNhapHang Object.
        /// </summary>
        /// <param name="obj">Object Insert.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
        public static bool Insert(PhieuNhapHang obj)
        {
            bool result = false;
            String methodName = "InsertPhieuNhapHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spInsertPhieuNhapHang");

            //Add value "NguoiNhap"  paramater.
            ManagerDao.AddInParameter(cmd, "NguoiNhap", DbType.String,
                ConvertValueObject.CastObjValueToInsertDB(obj.NguoiNhap));
            //Add value "MaKho"  paramater.
            ManagerDao.AddInParameter(cmd, "MaKho", DbType.Int32,
                ConvertValueObject.CastObjValueToInsertDB(obj.MaKho));
            //Add value "NgayNhap"  paramater.
            ManagerDao.AddInParameter(cmd, "NgayNhap", DbType.DateTime,
                ConvertValueObject.CastObjValueToInsertDB(obj.NgayNhap));
            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                Int32 id = Int32.Parse(ManagerDao.GetParameterValue(cmd, "returnValue").ToString());

                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }

        /// <summary>
        /// Define function Update for PhieuNhapHang Object.
        /// </summary>
        /// <param name="obj">Object Update.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
        public static bool Update(PhieuNhapHang obj)
        {
            bool result = false;
            String methodName = "UpdatePhieuNhapHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spUpdatePhieuNhapHang");


            //Add value "MaPhieuNhapHang"  paramater.
            ManagerDao.AddInParameter(cmd, "MaPhieuNhapHang", DbType.Int64, ConvertValueObject.CastObjValueToInsertDB(obj.MaPhieuNhapHang));
            //Add value "NguoiNhap"  paramater.
            ManagerDao.AddInParameter(cmd, "NguoiNhap", DbType.String, ConvertValueObject.CastObjValueToInsertDB(obj.NguoiNhap));
            //Add value "MaKho"  paramater.
            ManagerDao.AddInParameter(cmd, "MaKho", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(obj.MaKho));
            //Add value "NgayNhap"  paramater.
            ManagerDao.AddInParameter(cmd, "NgayNhap", DbType.DateTime, ConvertValueObject.CastObjValueToInsertDB(obj.NgayNhap));
            // add parameter store return value.
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                int rows = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (rows == 1)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }

            }
            catch (Exception e)
            {
                //write error Message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }
        /// <summary>
        /// Get Data PhieuNhapHang object. 
        /// </summary>
        /// <param name="maPhieuNhapHang" >This is primary key to get data.</param>
        /// <returns>PhieuNhapHang object.</returns>

        public static PhieuNhapHang Get(Int64 maPhieuNhapHang)
        {
            String methodName = "GetPhieuNhapHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                PhieuNhapHang result = null;
                DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spGetPhieuNhapHang");
                //Add value "MaPhieuNhapHang"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "MaPhieuNhapHang", DbType.Int64, maPhieuNhapHang);
                IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
                if (reader.Read())
                {
                    result = GetPhieuNhapHangByDataReader(reader);

                }
                reader.Close();
                return result;
            }
            catch (System.Exception e)
            {
                //write error message to log file.
                logger.Error("Get", e);
                return null;
            }
        }
        /// <summary>
        /// Get All PhieuNhapHang object from table PhieuNhapHang.
        /// </summary>
        /// <returns>List PhieuNhapHang object.</returns>
        public static List<PhieuNhapHangResult> GetAll()
        {
            String methodName = "GetAll";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<PhieuNhapHangResult> listPhieuNhapHang = new List<PhieuNhapHangResult>();
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetAllPhieuNhapHang");

                using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        listPhieuNhapHang.Add(GetPhieuNhapHangResultByDataReader(reader));
                    }
                    reader.Close();
                }

                return listPhieuNhapHang;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }

        /// <summary>
        /// Search PhieuNhapHang object in table PhieuNhapHang with condition is t object.
        /// </summary>
        /// <param name="t">Condition for Search.</param>       
        /// <param name="totalRow">  total records in this table.</param>
        /// <returns>List PhieuNhapHangResult object.</returns>
       
        /// <summary>
        /// Delete PhieuNhapHang object. 
        /// </summary>
        /// <param name="MaPhieuNhapHang" >This is primary key to delete.</param>
        /// <returns>True/False.</returns>

        public static bool Delete(Int64 MaPhieuNhapHang)
        {
            String methodName = "DeletePhieuNhapHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            ManagerDao.BeginTransaction();
            //get MaKho Cu
            PhieuNhapHang objOldPhieuNhap = Get(MaPhieuNhapHang);
            if (objOldPhieuNhap == null)
            {
                ManagerDao.Rollback();
                return false;
            }
            if (objOldPhieuNhap.IsCommit)
            {
                ManagerDao.Rollback();
                return false;
            }
            try
            {
                //delte Nha Cung Cap
                if (objOldPhieuNhap.MaNhaCC > 0)
                {
                    if (!NhaCungCapDao.Delete(objOldPhieuNhap.MaNhaCC))
                    {
                        ManagerDao.Rollback();
                        return false;
                    }
                }
                

                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spDeletePhieuNhapHang");
                //Add value "MaPhieuNhapHang"  paramater.
                ManagerDao.AddInParameter(dbCommand, "MaPhieuNhapHang", DbType.Int64, MaPhieuNhapHang);
                //Add value returnValue  paramater.
                ManagerDao.AddParameter(dbCommand, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
                try
                {
                    ManagerDao.ExecuteNonQuery(dbCommand);
                    int parameterValue = (int)ManagerDao.GetParameterValue(dbCommand, "returnValue");
                    if (parameterValue == 1)
                    {                       
                        ManagerDao.Commit();
                        return true;
                    }
                    else {
                        ManagerDao.Rollback();
                        return false;
                    }
                }
                catch (Exception)
                {
                    
                }               
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
               
            }
            ManagerDao.Rollback();
            return false;
        }
        /// <summary>
        /// Read data from IDataReader, convert to PhieuNhapHangResult Object.
        /// </summary>
        private static PhieuNhapHangResult GetPhieuNhapHangResultByDataReaderByLanguage(IDataReader reader, string LanguageId)
        {
            String methodName = "GetPhieuNhapHangResultByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                PhieuNhapHangResult result = new PhieuNhapHangResult();
                //read MaPhieuNhapHang field data.
                result.MaPhieuNhapHang = ManagerDao.Getint64DataFromDataReader(reader, "MaPhieuNhapHang");

                //read NguoiNhap field data.
                result.NguoiNhap = ManagerDao.GetstringDataFromDataReader(reader, "NguoiNhap");

                //read MaKho field data.
                result.MaKho = ManagerDao.GetintDataFromDataReader(reader, "MaKho");

                //read NgayNhap field data.
                result.NgayNhap = ManagerDao.GetdatetimeDataFromDataReader(reader, "NgayNhap");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        /// <summary>
        /// Read data from IDataReader, convert to PhieuNhapHangResult Object.
        /// </summary>
        protected static PhieuNhapHangResult GetPhieuNhapHangResultByDataReader(IDataReader reader)
        {
            String methodName = "GetPhieuNhapHangResultByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                PhieuNhapHangResult result = new PhieuNhapHangResult();
                //read MaPhieuNhapHang field data.
                result.MaPhieuNhapHang = ManagerDao.Getint64DataFromDataReader(reader, "MaPhieuNhapHang");

                //read NguoiNhap field data.
                result.NguoiNhap = ManagerDao.GetstringDataFromDataReader(reader, "NguoiNhap");

                //read TenKho field data.
                result.TenKho = ManagerDao.GetstringDataFromDataReader(reader, "TenKho");

                //read MaKho field data.
                result.MaKho = ManagerDao.GetintDataFromDataReader(reader, "MaKho");

                //read ThanhTien field data.
                result.ThanhTien = ManagerDao.GetdoubleDataFromDataReader(reader, "ThanhTien");
                //double a = ManagerDao.GetdoubleDataFromDataReader(reader, "ThanhTien");

                //read NgayNhap field data.
                result.NgayNhap = ManagerDao.GetdatetimeDataFromDataReader(reader, "NgayNhap");
                result.IsCommit = ManagerDao.GetbooleanDataFromDataReader(reader, "IsCommit");
                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }

    }
}